package reflect;

import java.lang.reflect.Constructor;

/**
 * 使用有参构造器实例化对象
 */
public class ReflectDemo3 {
    public static void main(String[] args) throws Exception {
        Person p = new Person("张三");
        System.out.println(p);
        //获取类对象
        /*Class cls = Class.forName("reflect.Person");
        Constructor c = cls.getConstructor(String.class);
        Object obj = c.newInstance("李四");
        System.out.println(obj);

        Constructor c1 = cls.getConstructor(String.class,int.class);
        Object obj1 = c1.newInstance("美美",18);
        System.out.println(obj1);*/
        Class cls = Class.forName("reflect.Person");
        Constructor c1 = cls.getConstructor(String.class);
        Object obj1 = c1.newInstance("三少");
        System.out.println(obj1);
        Constructor c2 = cls.getConstructor(String.class,int.class);
        Object obj2 = c2.newInstance("美美",18);
        System.out.println(obj2);



    }

}
